package com.lyfam.vaadin.view.form;

import java.util.Iterator;
import java.util.List;

import com.lyfam.annotation.listener.Listener;
import com.lyfam.vaadin.formfactory.DealFormFactory;
import com.lyfam.vaadin.service.FakeServices;
import com.vaadin.data.Container;
import com.vaadin.data.util.IndexedContainer;

public class DealSubProductValueChangedListener implements Listener<DealFormFactory>
{
    @Override
    public void process(DealFormFactory form, Object value)
    {
        List<String> related = FakeServices.getRelatedProductList((String) value);
        
        if (related.size() > 0)
        {
            form.getrelatedProduct().getContainerDataSource().removeAllItems();
            form.getrelatedProduct().setValue(null);
            Container c = new IndexedContainer();
            for (final Iterator i = related.iterator(); i.hasNext();)
            {
                c.addItem(i.next());
            }
            
            form.getrelatedProduct().setContainerDataSource(c);
            form.getrelatedProduct().setVisible(true);
        }
        else
        {
            form.getrelatedProduct().setValue(null);
            form.getrelatedProduct().setVisible(true);
            form.getrelatedProduct().setVisible(false);
        }
        
    }

}
